// const api = require('../../utils/api');
import { login } from '../../utils/api';

const state = {
	token: '',
	name: '',
	avatar: '',
	userId: null
}

const mutations = {
	SET_USERID: (state, userId) => {
		state.userId = userId
	},
	SET_NAME: (state, name) => {
		state.name = name
	},
	SET_AVATAR: (state, avatar) => {
		state.avatar = avatar
	}
}

const actions = {
	// user login
	loginState({commit}, userInfo) {
		return new Promise((resolve, reject) => {
			login(userInfo).then(res => {
				if (res.data.status == 0 && res.data.data) {
					commit('SET_USERID', res.data.data.UserID);
					commit('SET_NAME', res.data.data.UserName);
					commit('SET_AVATAR', res.data.data.UserHead);
					uni.setStorageSync('userId', res.data.data.UserID);
				}
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},

	// user logout
	logout({commit}) {
		return new Promise(resolve => {
			commit('SET_USERID', null);
			commit('SET_NAME', null);
			commit('SET_AVATAR', null);
			resolve()
		})

	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
